Column

Orders by Time of Day

Column

Orders by Department

When shoppers buy their produce

---
title: "Instacart Dashboard"
output: 
  flexdashboard::flex_dashboard:
    navbar: 
      - { title: "Home", href: "https://shelley-shen.github.io/index.html", align: left}
    orientation: columns
    vertical_layout: fill
    source: embed
---

```{r setup, include=FALSE}
library(tidyverse)
library(p8105.datasets)
library(plotly)
library(flexdashboard)
```


```{r include=FALSE}
data("instacart")

instacart = 
  instacart %>%
  select(-add_to_cart_order, -eval_set, -order_number, -days_since_prior_order, -aisle_id, -department_id) 
```

Column {data-width=625}
-----------------------------------------------------------------------

### Orders by Time of Day
```{r echo=FALSE, warning=FALSE}
instacart %>% 
  mutate(
    order_dow = recode(order_dow, "0" = "Sunday", "1" = "Monday", "2" = "Tuesday", "3" = "Wednesday", "4" = "Thursday", "5" = "Friday", "6" = "Saturday"),
    order_dow = factor(order_dow, levels = c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"))
    ) %>% 
  group_by(order_dow, order_hour_of_day) %>% 
  summarize(hourly_orders = n()) %>% 
  mutate(
    text_label = str_c("Hour: ", order_hour_of_day, "\nDay: ", order_dow, "\n# of orders: ", hourly_orders)
  ) %>% 
  plot_ly(
    x = ~order_hour_of_day, 
    y = ~hourly_orders, 
    color = ~order_dow, 
    type = "scatter", 
    mode = "line",
    text = ~text_label,
    colors = "viridis") %>% 
  layout(
    xaxis = list(title = "Hour of Day"),
    yaxis = list(title = "Number of Orders"))
```


Column {data-width=375}
-----------------------------------------------------------------------

### Orders by Department

```{r echo=FALSE}
instacart %>%
  count(department) %>% 
  filter(department != "missing") %>% 
  mutate(
    department = fct_reorder(department, n)) %>% 
  plot_ly(
    x = ~department, y = ~n, color = ~department, type = "bar", colors = "magma") %>% 
  layout(
    xaxis = list(title = "Department"),
    yaxis = list(title = "Number of orders"))
```

### When shoppers buy their produce

```{r echo=FALSE}
instacart %>% 
  filter(department == "produce") %>% 
  mutate(aisle = fct_reorder(aisle, order_hour_of_day)) %>%
  plot_ly(x = ~aisle, y = ~order_hour_of_day, color = ~aisle, type = "box", colors = "viridis") %>% 
  layout(
    xaxis = list(title = "Produce Aisle"),
    yaxis = list(title = "Hour of Day"))
```